#!/usr/bin/env python
"""
Nagios plugin that monitors the core vash services. 
Notice, the pid location paths are hardcoded. 
"""
__author__="Placi Flury grid@switch.ch"
__copyright__ = "Copyright 2010, SMSCG an AAA/SWITCH project"
__date__="12.11.2008"
__version__="0.1.0"

import sys
import os.path
from nagios_plugin import NagiosPluginBasic,LocalService
from nagios_plugin import RETURN_CODE


MYSQL_PID_LOCATION = "/var/run/mysqld"
TOMCAT_PID_LOCATION = "/usr/share/tomcat5/temp"
TOMCAT_VASH_LOCATION="/var/lib/tomcat5/webapps/vash"
VASH_INSTANCES = ["smscg","switch","crypto"]

class VOMSCore(NagiosPluginBasic):
    """
    Checks whether GridFtp service is running. It's a local check
    without any connectivity testing.
    """
    def __init__(self):
        parser = NagiosPluginBasic.getOptions(self)
        parser.remove_option("-c")   # removing thresholds
        parser.remove_option("-w")
        parser.remove_option("-H")   # remove hostname option
        parser.parse_args()


    def main(self):
        service = LocalService("mysqld",MYSQL_PID_LOCATION)       
        msg, return_code = service.getstate()
        if return_code != RETURN_CODE["ok"]:
            print msg
            sys.exit(return_code)
        
        service = LocalService("apache2")
        msg, return_code = service.getstate()
        if return_code != RETURN_CODE["ok"]:
            print msg
            sys.exit(return_code)

        service = LocalService("tomcat5",TOMCAT_PID_LOCATION)
        msg, return_code = service.getstate()
        if return_code != RETURN_CODE["ok"]:
            print msg
            sys.exit(return_code)
       
        # vo instances checks 
        for vash in VASH_INSTANCES:
            vash_loc = os.path.join(TOMCAT_VASH_LOCATION, vash)
            if not os.path.exists(vash_loc):
                print "SERVICE CRITICAL: VASH instance '%s' is not up." % vash
                sys.exit(RETURN_CODE["critical"])

        print "SERVICE OK: Core VASH services and VASH Instance(s) %s are up and running." % str(VASH_INSTANCES)
        sys.exit(RETURN_CODE["ok"])


if __name__ =="__main__":
    g = VOMSCore()
    g.main()
